package com.z.admin.controller;

import com.z.common.config.security.LoginUser;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class IndexController {

    @GetMapping("login.html")
    public String toString() {
        return "login";
    }

    @GetMapping({"/", "index"})
    public String index(Model model) {
        boolean isLogin = false;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            LoginUser user = (LoginUser)principal;
            model.addAttribute("user", user);
            isLogin = true;
        }
        model.addAttribute("isLogin", isLogin);

        return "index";
    }
}
